package com.example.demo.Controller;

import com.example.demo.model.Friend;
import com.example.demo.model.FriendMapper;
import com.example.demo.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendController {
   @Resource
//   操作的数据库资源
    private FriendMapper friendMapper;
   @GetMapping("/friendList")
   @ResponseBody
    public Object getFriendList(HttpServletRequest req){
//       先从会话获取到userId是啥
       HttpSession session = req.getSession(false);
       if(session == null){
           //当前用户会话不存在，则是未登录，则返回空列表
           System.out.println("[getFriendList]session 不存在");
           return  new ArrayList<Friend>();

       }
       User user =(User)session.getAttribute("user");
        if(user == null){
            //当前对象没在会话中存在
            System.out.println("[getFriendList] user 不存在");
            return new ArrayList<Friend>();
        }
        //根据userId从数据库查数据即可
       List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;
   }
}
